$(function(){
//------------------------------------------------------------------
//show suggestion
//------------------------------------------------------------------
	$("#town").autocomplete({
		source: function(request, response) {
			var keyword = request.term;
			$.getJSON(AJAX_URL + 'town.ajax.php?keyword='+keyword+'&limit='+MAX_SUGGESTION, function(towns){
				response( $.map(towns, function(town) {
					return {
						label: town.name+' '+'('+town.code+')',
						value: town.name+' '+'('+town.code+')'
					}
				}));
			});
		},
		minLength: 1
	});

//------------------------------------------------------------------
// submit
//------------------------------------------------------------------
	$('#btn-submit').bind('click', function(){
		$('#new-form').submit();
	});
	
//------------------------------------------------------------------
//when one file is selected then submit the form and show the preview
//------------------------------------------------------------------
	$('#photo').bind('change', function(){
		$('#upload-photo-form .error').text('');
		$('#upload-photo-form').submit();
		$('#upload-loading').html('<img src="/images/iconLoading.gif" />');
	});
	
//------------------------------------------------------------------
//remove photo when double click
//------------------------------------------------------------------
	$('#photos span.photo').livequery('dblclick', function(){
		$(this).remove();
		if ($('#photos span.photo').length == 0) {
			$('#note-delete-photo').text('');
		}
	});
});
//------------------------------------------------------------------
//style the select box and input file
//------------------------------------------------------------------
	/*
	$('select, input:file').uniform();
	$('#uniform-categoryId').css('width', '160px');
	$('#uniform-photo').css('width', '250px');
	*/
/*$(function() {
  //switch autocomplete of locality to off
  $('#locality').attr('autocomplete', 'off');

  //fill the locality box when click on the selectbox-------------------------
  $('select#localities option').livequery('click', function(){
	var locality = $(this).html();
	
	//fill townName to locality box
	$('#locality').val(locality);
	
	//off the suggestion
	$('#locality_suggestion').html('');
  });
	
  //show the locality suggestion on keyup-------------------------------------
  $("#locality").bind('keyup', function(){
    var keyword = $(this).val();
	
    //start only when the keyword >= 3
    if (keyword.length < 3) {
      $('#locality_suggestion').html('');
      return false;
    }

    //add option
    $.getJSON(AJAX_URL + 'locality.ajax.php?keyword='+keyword+'&limit='+MAX_SUGGESTION, function(towns){
      //if there is no result then show no result
      if (towns.length == 0) {
        $('#locality_suggestion').html('<div style="text-align:center;border:1px solid #cccccc;width:255px;background-color:white;"><i>Không có vùng nào tương ứng</i></div>');
        return false;
      }
    	
      //calculate selectbox size
	  var selectBoxSize = (towns.length < MAX_SUGGESTION) ? towns.length : MAX_SUGGESTION;
	  
      //create the select box
      $localitySuggestion = '<select style="width:275px;border: 1px solid #cccccc;" id="localities" size="'+selectBoxSize+'">';

      //add options to selectbox
	  for(var i=0; i<towns.length; i++){
		$localitySuggestion += "<option value='"+towns[i]['code']+"'>"+"("+towns[i]['code']+")"+" "+towns[i]['name']+"</option>";
      }
	  
	  $localitySuggestion += '</select>';
	  
	  //show the suggestion
	  $('#locality_suggestion').html($localitySuggestion);
    });
    
    return false;
  });
});
*/

//------------------------------------------------------------------
//Change towns when departement is changed
//------------------------------------------------------------------
/*
	$('#departementCode').bind('change', function(){
		$('#townId').html('');
		var departementCode = $(this).val();
		$.getJSON(CACHE_URL + 'departement/' + departementCode + '.towns.json', function(towns){
			for(var $i=0; $i<towns.length;$i++) {
				var town = $('<option />',{
							 	'id' : towns[$i].id,
							 	html : towns[$i].name + ' (' + towns[$i].code + ')'
						   }).appendTo($('#townId'));
			}
		});
	});
*/